package com.hsteach.service.meeting;

import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.dao.domain.meeting.MeetingFile;
import com.hsteach.dao.domain.meeting.MeetingSummary;
import com.hsteach.dao.mapper.meeting.MeetingFileMapper;
import com.hsteach.dao.mapper.meeting.MeetingSummaryMapper;
import com.hsteach.web.controller.meeting.dto.MeetingSummaryDto;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 会议纪要
 */
@Service
public class MeetingSummaryService {

    @Resource
    private MeetingSummaryMapper meetingSummaryMapper;

    @Resource
    private MeetingFileMapper meetingFileMapper;

    private static final String SUMMARY = "summary";

    /**
     * 查询会议纪要列表
     *
     * @param applyId
     * @return
     */
    public List<MeetingSummary> list(Long applyId) {
        List<MeetingSummary> list = meetingSummaryMapper.selectList(applyId);
        return list;
    }

    /**
     * 新增会议纪要
     *
     * @param dto
     * @return
     */
    public Long add(MeetingSummaryDto dto) {
        MeetingSummary meetingSummary = new MeetingSummary();
        meetingSummary.setApplyId(dto.getApplyId());
        meetingSummary.setPersonId(SecurityUtils.getAuthInfo().getPersonId());
        meetingSummary.setPersonName(SecurityUtils.getAuthInfo().getName());
        meetingSummary.setSummaryContent(dto.getSummaryContent());
        meetingSummary.setOrgCode(SecurityUtils.getAuthInfo().getOrgCode());
        Long num = meetingSummaryMapper.insert(meetingSummary);

        if (num == 1 && !ObjectUtils.isEmpty(dto.getFileVos()) && dto.getFileVos().size() > 0) {
            MeetingFile meetingFile = new MeetingFile();
            dto.getFileVos().forEach(file -> {
                meetingFile.setCorrelationId(meetingSummary.getId());
                meetingFile.setFileType(file.getFileType());
                meetingFile.setUrl(file.getFileUrl());
                meetingFile.setBelongTo(SUMMARY);
                meetingFileMapper.insert(meetingFile);
            });
        }
        return num;
    }
}
